package com.best.oasis.settlement.util.security;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.ldap.LdapUserDetailsImpl;
import org.springframework.security.userdetails.ldap.UserDetailsContextMapper;

import com.best.oasis.settlement.common.entity.security.User;


/**
 * 从best logistics ldap 获取的用户信息映射到User类的映射类
 * @author BL00064
 *
 */
public class BestLdapUserDetailsMappper implements UserDetailsContextMapper{
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(BestLdapUserDetailsMappper.class);

	@Override
	public User mapUserFromContext(DirContextOperations ctx,
			String username, GrantedAuthority[] authority) {
		User result = new User();
		result.setUserName(username);
		String dn = ctx.getNameInNamespace();

        logger.debug("Mapping user details from context with DN: " + dn);

        LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
        essence.setDn(dn);

        // result.setName(ctx.getStringAttribute("name")); 这里的name是英文名或者中文名的拼音
        result.setName(ctx.getStringAttribute("description"));
        result.setProvince(ctx.getStringAttribute("st"));
        result.setCity(ctx.getStringAttribute("l"));
        result.setDepartment(ctx.getStringAttribute("department"));
        result.setPostCode(ctx.getStringAttribute("postalcode"));
        result.setTelephoneExtention(ctx.getStringAttribute("telephoneNumber"));
        result.setEmail(ctx.getStringAttribute("mail"));
		result.setMobile(ctx.getStringAttribute("mobile"));
		return result;
	}

	@Override
	public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
		// TODO Auto-generated method stub
		
	}

}
